package com.zhao.dota.struct;

import lombok.Data;
import org.springframework.util.StringUtils;

@Data
public class Result<T> {

    private T data;

    private int code;

    private String message;

    public static <T> Result<T> complete(boolean succeed) {
        return complete(succeed, null);
    }

    public static <T> Result<T> complete(boolean succeed, String message) {
        if (succeed) {
            return success(null, message);
        }
        return error(message);
    }

    public static <T> Result<T> success() {
        return success(null);
    }

    public static <T> Result<T> success(T data) {
        return success(data, null);
    }

    public static <T> Result<T> success(T data, String message) {
        Result<T> result = new Result<>();
        result.setData(data);
        result.setCode(100);
        result.setMessage(message);
        return result;
    }

    public static <T> Result<T> error() {
        return error(null);
    }

    public static <T> Result<T> error(int code) {
        return error(code, null);
    }

    public static <T> Result<T> error(String message) {
        return error(200, message);
    }

    public static <T> Result<T> error(int code, String message) {
        return error(code, message, null);
    }

    public static <T> Result<T> error(int code, String message, T data) {
        Result<T> result = new Result<>();
        result.setData(data);
        result.setCode(code);
        if (!StringUtils.hasText(message)) {
            message = "系统错误";
        }
        result.setMessage(message);
        return result;
    }

}
